package com.shai.wool.message.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.shai.wool.common.controller.BaseController;

import com.shai.wool.message.service.SysMessageService;

import com.shai.wool.message.entity.SysMessageVO;
import com.shai.wool.message.param.SysMessageParam;

/**
 * 发送的站内信信息Controller
 *
 * @author 朱国军
 * @since 1.0
 * @version 2015年12月27日 朱国军
 */
@Controller
public class SysMessageController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(SysMessageController.class);

    @Autowired
    private SysMessageService sysMessageService;

    private final static String LIST_PAGE = "/sysMessage/list";

    private final static String VIEW_PAGE = "/sysMessage/view";

    private final static String UPDATE_PAGE = "/sysMessage/update";
    
    @RequestMapping(value = "/check/sysMessage/toInsert")
    public ModelAndView intoInsert() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        SysMessageVO sysMessageVO = new SysMessageVO();
        resultMap.put("sysMessageVO", sysMessageVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/check/sysMessage/toUpdate/{id}")
    public ModelAndView intoUpdate(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        SysMessageVO sysMessageVO = sysMessageService.findById(id);
        resultMap.put("sysMessageVO", sysMessageVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/check/sysMessage/update",produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String update(SysMessageVO sysMessageVO) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer insertCount = sysMessageService.update(sysMessageVO);
            if (insertCount != null && insertCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "新增或修改发送的站内信信息成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "新增或修改发送的站内信信息失败");
            }
        } catch (Exception e) {
            logger.error("新增或修改发送的站内信信息出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "新增或修改发送的站内信信息出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
    
    @RequestMapping(value = "/check/sysMessage/delete/{id}", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String delete(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer deleteCount = sysMessageService.delete(id);
            if (deleteCount != null && deleteCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "删除发送的站内信信息成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "删除发送的站内信信息失败");
            }
        } catch (Exception e) {
            logger.error("删除发送的站内信信息出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "删除发送的站内信信息出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
       
    @RequestMapping(value = "/uncheck/sysMessage/list")
    public ModelAndView queryList(SysMessageParam sysMessageParam) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<SysMessageVO> dataList = sysMessageService.queryByPage(sysMessageParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", sysMessageParam);
        return new ModelAndView(LIST_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/uncheck/sysMessage/view/{id}")
    public ModelAndView viewConfigInfo(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        SysMessageVO sysMessageVO = sysMessageService.findById(id);
        resultMap.put("sysMessageVO", sysMessageVO);
        return new ModelAndView(VIEW_PAGE, resultMap);
    }
}